/*
 * Copyright (c) 2023 fred
 * Licensed under the Apache License,Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
import { OutputSettings } from './OutputSettings';
import { Attributes } from './Attributes';
import { LeafNode } from './LeafNode';
import List from '@ohos.util.List';
import { Node } from './Node';
import { Element } from './Element';
import { XmlDeclaration } from './XmlDeclaration';

/**
 A comment node.
 */
export class Comment extends LeafNode {
  constructor(data:string) {
    super()
    this.value = data
  }

  public nodeName():string {
    return "#comment"
  }

  /**
   Get the contents of the comment.
   @return comment content
   */
  public getData():string {
    return this.coreValue()
  }

  public setData(data:string):Comment {
    this.setCoreValue(data)
    return this
  }

  outerHtmlHead(accum: string[], depth: number, out: OutputSettings) {
    if (out.getPrettyPrint() && ((this.isEffectivelyFirst() &&
    this.parentNode instanceof Element && (this.parentNode as Element).getTag().formatAsBlock()) ||
    out.getOutline())) {
      this.indent(accum, depth, out)
    }
    accum.push("<!--")
    accum.push(this.getData())
    accum.push("-->")
  }

  outerHtmlTail(accum: string[], depth: number, out: OutputSettings) {}

  public toString():string {
    return this.outerHtml()
  }

  public clone():Comment{
    return super.clone() as Comment
  }

  /**
   * Check if this comment looks like an XML Declaration.
   * @return true if it looks like, maybe, it's an XML Declaration.
   */
  public isXmlDeclaration():boolean {
    let data = this.getData()
    return Comment.isXmlDeclarationData(data)
  }

  private static isXmlDeclarationData(data:string):boolean {
    return data.length > 1 && (data.startsWith("!") || data.startsWith("?"))
  }

  /**
   * Attempt to cast this comment to an XML Declaration node.
   * @return an XML declaration if it could be parsed as one, null otherwise.
   */
  public asXmlDeclaration():XmlDeclaration | null{
    //todo
    return null
  }

}

